<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>申领单开票</title>
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--{include file='comm/comm_pic.tpl'}-->
    <style type="text/css">
        .items2 ul{ }
        .items2 ul li{margin-bottom: 10px;border: 1px solid #ffffff;height: 30px;line-height: 30px;padding: 0 5px;}
        .items2 ul li.selected {background-color: #E8F1FF;border: 1px solid #B2D1FF;box-shadow: 2px 2px 0 #E5E5E5;font-weight: bold;}
    </style>
</head>

<body>

<div class="container">

<div style="width:98%; margin: 0 auto;">
    <div class="title">
        <h2 class="tac">
            <!--{$card_apply_array.apply_id}--> - 申领单开票
        </h2>
    </div>

    <div class="panel panel-default">
        <h2 class="panel-heading">申领单信息</h2>
        <div class="items2">
            <table class="form-inline">
                <tbody>
                <tr style="height: 40px;">
                    <td class="tal">客户名称：<input type="text" class="form-control w150" name="customer_name" value="<!--{$card_apply_array.customer_name}-->" readonly="readonly"></td>
                    <td class="tal">申请日期：<input type="text" class="form-control w150" name="apply_date" value="<!--{$card_apply_array.apply_date}-->" readonly="readonly"></td>
                </tr>
                <tr style="height: 40px;">
                    <td class="tal">联 系 人：<input type="text" class="form-control w150" name="linkman" value="<!--{$card_apply_array.linkman}-->" readonly="readonly"></td>
                    <td class="tal">联系方式：<input type="text" class="form-control w150" name="contact_way" value="<!--{$card_apply_array.contact_way}-->" readonly="readonly"></td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>


    <!--现金卡信息-->

    <div class=" panel panel-default">
        <h2 class="panel-heading">
            现金卡信息
        </h2>
        <table class="table">
            <thead>
            <tr>

            </tr>
            </thead>
            <thead>
            <tr>
                <th>面值</th>
                <th>规格</th>
                <th>数量</th>
                <th>已发数量</th>
                <th>折扣率</th>
                <th>折后价</th>
                <td>代金卡类型</td>
                <th>备注</th>
            </tr>
            </thead>

            <tbody>
            <!--{foreach from=$card_detail_array item=card_detail}-->
            <tr>
                <td><!--{$card_detail.face_value}--></td>
                <td><!--{$card_detail.card_spec}--></td>
                <td><!--{$card_detail.card_num}--></td>
                <td><!--{$card_detail.send_card_num}--></td>
                <td><!--{$card_detail.card_discount}--></td>
                <td><!--{$card_detail.card_total_amount}--></td>
                <td><!--{$card_detail.card_type}--></td>
                <td><!--{$card_detail.remark}--></td>
            </tr>
            <!--{/foreach}-->
            </tbody>

        </table>
    </div>

    <form id="item_form" onsubmit="return false;" class="form-horizontal">
        <input type="hidden" name="invoice_id" id="invoice_id" value="0">
        <div class=" panel panel-default">
            <h2 class="panel-heading">
                开票信息
            </h2>
            <div class="items2">
                <table class="form-inline">
                    <tbody>
                        <tr style="height: 40px;">
                            <td class="tal">
                                <label for="invoiceAmount">
                                    可开票金额：
                                </label>
                                <input type="text" class="form-control w150" id="invoiceAmount" readonly="readonly" name="invoiceAmount" value="<!--{$card_apply_array.total_amount-$card_apply_array.invoice_amount|string_format:'%.2f'}-->" />
                            </td>
                        </tr>
                        <tr style="height: 40px;">
                            <td class="tal">
                                <label for="invoice_title">
                                    &nbsp;&nbsp;发票抬头：
                                </label>
                                <input type="text" class="form-control w300" id="invoice_title" value="" placeholder="请输入发票抬头" name="invoice_title" value="" />
                            </td>
                        </tr>
                        <tr style="height: 40px;">
                            <td class="tal">
                                <label for="invoice_content">
                                    &nbsp;&nbsp;发票内容：
                                </label>
                                <input type="text" class="form-control w300" id="invoice_content" name="invoice_content" value="" placeholder="请输入发票内容" />
                            </td>
                        </tr>
                        <tr style="height: 40px;">
                            <td>
                                <label for="invoice_amount">
                                    &nbsp;&nbsp;发票金额：
                                </label>
                                <input type="text" class="form-control w150" id="invoice_amount" name="invoice_amount" value="" placeholder="请输入发票金额">
                            </td>
                        </tr>
                        <tr style="height: 40px;">
                            <td>
                                <label for="invoice_amount">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;备 注：
                                </label>
                                <textarea id="remark" class="form-control w300" rows="3" name="remark"  placeholder="请输入备注"></textarea>
                            </td>
                        </tr>

                        <tr style="height: 40px;">
                            <td>
                                <button type="button" class="btn btn-success" onclick="cardApplyList.invoice();">新增发票</button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </form>

    <div class="panel panel-default">
        <div class="panel-heading">开票列表</div>
        <table class="table">
            <thead>
            <tr>
                <th width="*">发票抬头</th>
                <th width="100">发票金额</th>
                <th width="260">发票内容</th>
                <th width="300">备注</th>
                <th width="360">发票编号</th>
            </tr>
            </thead>
            <tbody class="form-inline" id="invoice_list">
            <!--{if $apply_invoice_array}-->
                <!--{foreach from=$apply_invoice_array item=invoice}-->
                    <tr>
                        <td><!--{$invoice.invoice_title}--></td>
                        <td><!--{$invoice.invoice_amount|string_format:'%.2f'}--></td>
                        <td><!--{$invoice.invoice_content}--></td>
                        <td><!--{$invoice.remark}--></td>
                        <td id="invoice_no_line_<!--{$invoice.invoice_id}-->">
                            <!--{if $invoice.status == 1}-->
                                <input type="text" class="form-control input-sm w150" id="invoice_no_<!--{$invoice.invoice_id}-->" placeholder="请输入发票号"/>
                                <!--{if $outInvoice_permission}-->
                                    <input type="button" class="btn btn-primary btn-sm" value="确认开票" onclick="cardApplyList.confirmInvoice(<!--{$invoice.invoice_id}-->);" />
                                    &nbsp;
                                    <input type="button" class="btn btn-primary btn-sm" value="删除" onclick="cardApplyList.delApply_invoice(<!--{$invoice.invoice_id}-->);" />
                                <!--{/if}-->
                            <!--{else}-->
                                <!--{$invoice.invoice_no}-->
                            <!--{/if}-->
                        </td>
                    </tr>
                <!--{/foreach}-->
            <!--{/if}-->
            </tbody>
        </table>
    </div>

</div>
<script type="text/javascript">


    $(window).ready(function() {

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });

    });

    var cardApplyList = {

        invoice_id:'',
        apply_id: <!--{$card_apply_array.apply_id}-->,

        invoice: function(){

            this.dt = E.getFormValues('item_form');
            var error_msg = '';

            if (E.isEmpty(this.dt.invoice_amount)){
                error_msg += '开票金额不能为空<br/>';

            } else if(!E.isMoney(this.dt.invoice_amount)){
                error_msg += '开票金额错误<br/>';
            }

            if(E.isEmpty(this.dt.invoice_content))
                error_msg += '开票内容不能为空<br/>';

            if(E.isEmpty(this.dt.invoice_title))
                error_msg += '开票抬头不能为空<br/>';

            if(parseFloat(this.dt.invoice_amount) > parseFloat(this.dt.invoiceAmount)){
                error_msg += '开票金额不能大于可开票金额<br/>';
            }

            if (error_msg != '') {
                E.alert(error_msg);
            }else{
                E.confirm('您确认新增申领单发票吗？', 'cardApplyList.execute');
            }

        },

        execute: function() {

            E.loadding.open('正在保存，请稍候...');

            E.ajax_post({
                url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=8',
                data:{
                    apply_id: this.apply_id,
                    invoice: this.dt
                },
                call: 'cardApplyList.result'
            });

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {

                var invoiceAmount = parseFloat( $("#invoiceAmount").val() );
                invoiceAmount = invoiceAmount - parseFloat( this.dt.invoice_amount );
                E.opener("cardApplyList.load");
                E.alert(o.message, 2);
                $("#invoiceAmount").val( invoiceAmount.toFixed(2) );
                $("#invoice_title").val('');
                $("#invoice_amount").val('');
                $("#invoice_content").val('');
                $("#remark").val('');

                var html = '<tr>';
                html += '<td>' + this.dt.invoice_title + '</td>';
                html += '<td>' + parseFloat(this.dt.invoice_amount).toFixed(2) + '</td>';
                html += '<td>' + this.dt.invoice_content + '</td>';
                html += '<td>' + this.dt.remark + '</td>';
                html += '<td id="invoice_no_line_' + o.data.invoice_id + '">';
                html += '<input type="text" class="form-control input-sm w150" id="invoice_no_' + o.data.invoice_id + '" placeholder="请输入发票号"/>';
                html += '&nbsp;<input type="button" class="btn btn-primary btn-sm" value="确认开票" onclick="cardApplyList.confirmInvoice(' + o.data.invoice_id + ');" />';
                html += '</td>';
                html += '</tr>';

                $('#invoice_list').append(html);

            } else {
                E.alert(o.message);
            }

        },

        confirmInvoice: function( invoice_id ) {

            var invoice_no = E.trim($('#invoice_no_' + invoice_id).val());
            if (E.isEmpty(invoice_no)) {
                E.alert('请输入发票编号', 1, function() {
                    $('#invoice_no_' + invoice_id).focus();
                });
            } else {

                E.confirm('您确认开票吗？', function() {

                    E.loadding.open('正在保存，请稍候...');
                    E.ajax_get({
                        url: '/invokingAjax-backend/company/cardApply.ajax?operFlg=9',
                        data: {
                            invoice_no: invoice_no,
                            invoice_id: invoice_id
                        },
                        call: function( o ) {

                            E.loadding.close();

                            if (o.code == 200) {

                                E.alert('确认开票成功', 2);
                                $('#invoice_no_line_' + invoice_id).text(invoice_no);

                            } else {
                                E.alert(o.message);
                            }

                        }
                    });

                });

            }

        },

        //开票删除
        delApply_invoice:function(t){
            var invoice_id = t;
                E.confirm('您确认删除吗？', function() {
                    E.loadding.open('正在保存，请稍候...');
                    E.ajax_get({
                        url: '/invokingAjax-backend/card/cardApply.ajax?operFlg=15',
                        data: {
                            invoiceID: invoice_id
                        },
                        call: function( o ) {
                            E.loadding.close();
                            if (o.code == 200) {
                                E.alert('删除成功', 2);
                                history.go(0);
                            } else {
                                E.alert(o.message);
                            }
                        }
                    });
                });


        }




    };


</script>



</body>
</html>
